// 账号
const loginIdValidator = new FieldValidator("txtLoginId", function (val) {
  if (!val) {
    return "账号不能为空";
  }
});

// 密码
const txtLoginPwdValidator = new FieldValidator("txtLoginPwd", function (val) {
  if (!val) {
    return "密码不能为空";
  }
});

const form = $(".user-form");
form.onsubmit = async function (e) {
  e.preventDefault(); //阻止浏览器默认跳转
  // 判断输入框规则是否正确
  const result = FieldValidator.validate(
    loginIdValidator,
    txtLoginPwdValidator
  );
  if (!result) {
    return; //验证未通过
  }
  const formData = new FormData(form);
  const data = Object.fromEntries(formData.entries());

  // 网络登录
  const resp = await API.login(data);
  if (resp.code === 0) {
    alert("登录成功，点击确定，跳转到登录页");
    location.href = "./index.html";
  } else {
    loginIdValidator.p.innerText = "账号或密码错误";
    txtLoginPwdValidator.input.value = "";
  }
};
